package com.cndongtai.service.net;

import java.util.Calendar;
import java.util.Date;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cndongtai.constant.LightConstant;
import com.cndongtai.dao.impl.LightCurrentRecordDao;
import com.cndongtai.dao.impl.LightDao;
import com.cndongtai.dao.impl.LightStatusRecordDao;
import com.cndongtai.net.NetController;
import com.cndongtai.net.message.Message;
import com.cndongtai.net.redis.RedisServer;
import com.cndongtai.po.Light;
import com.cndongtai.po.LightCurrentRecord;
import com.cndongtai.po.LightStatusRecord;
import com.cndongtai.util.DateUtil;

@Service("msgMonitor")
public class MsgMonitor {

	@Resource(name="lightStatusRecordDao")
	private LightStatusRecordDao lightStatusRecordDao;
	
	@Resource(name="lightDao")
	private LightDao lightDao;
	
	@Resource(name="lightCurrentRecordDao")
	private LightCurrentRecordDao lightCurrentRecordDao;
	
	private RedisServer redisServer = RedisServer.getInstance();
	
	public static boolean THREAD_START = false;
	
	@PostConstruct
	public void start() {
		if(!THREAD_START) {
			new MsgMonitorThread().start();
			THREAD_START = true;
		}
	}
	
	class MsgMonitorThread extends Thread {
		public void run() {
			while(true) {
				System.out.println("去上报数据开始=====");
				Message msg = NetController.pollMsg();
				System.out.println("去上报数据结束=====");
				if(msg!=null) {
					System.out.println("取到数据了=====");
					System.out.println("--------------msgType:" + msg.getType());
					if("38B7".equals(msg.getType())) {
						// RTU主动上报电流
						String lightNum = msg.getValue("lightNum");
						Light light = lightDao.queryLightByLightNum(lightNum);
						if(light==null) {
							continue;
						}
						
						Date collectTime = DateUtil.timeFormatBCD(msg.getValue("collectTime"));
						Calendar calendar = Calendar.getInstance();
						calendar.setTime(collectTime);
						
						int num = Integer.valueOf(msg.getValue("num"));
						for(int i=0; i<num; i++) {
							LightCurrentRecord current = new LightCurrentRecord();
							
							current.setLightId(light.getLightId());
							current.setCurrentType(msg.getValue("currentType"));
							calendar.add(Calendar.MINUTE, 15*i);
							current.setCollectTime(calendar.getTime());
							current.setWorkCurrent(Integer.valueOf(msg.getValue("workCurrent"+i)));
							
							lightCurrentRecordDao.addLightCurrent(current);
						}
						
					}else if("38B9".equals(msg.getType())) {
						// RTU主动上报状态
						LightStatusRecord status = new LightStatusRecord();
						
						String lightNum = msg.getValue("lightNum");
						String panId = msg.getValue("panId");
						Light light = lightDao.queryLightByLightNumAndPanId(panId,lightNum);
						if(light==null) {
							continue;
						}
						
						status.setLightId(light.getLightId());
						status.setCollectTime(DateUtil.timeFormatBCD(msg.getValue("collectTime")));
						status.setStatusCode(msg.getValue("statusCode"));
						status.setTemperature(Integer.valueOf(msg.getValue("temperature")));
						status.setSolarPanelVol(Integer.valueOf(msg.getValue("solarPanelVol")));
						status.setBatteryVol(Integer.valueOf(msg.getValue("batteryVol")));
						status.setChargeCurrent(Integer.valueOf(msg.getValue("chargeCurrent")));
						status.setWorkVol(Integer.valueOf(msg.getValue("workVol")));
						status.setWorkCurrent(Integer.valueOf(msg.getValue("workCurrent")));
						status.setChargeCount(Integer.valueOf(msg.getValue("chargeCount")));
						status.setWorkCount(Integer.valueOf(msg.getValue("workCount")));
						
						lightStatusRecordDao.addLightStatus(status);
						
						if("31".equals(msg.getValue("statusCode"))){
							lightDao.changeLightState(light.getLightId(), 1); 
						}else {
							lightDao.changeLightState(light.getLightId(), 0);
						}
						
						redisServer.set(LightConstant.REDIS_LIGHT_KEY+panId+"_"+lightNum, "1", 60*30);
					}
					
				}else {
					System.out.println("没取到数据了=====");
					System.out.println("MsgQueue is empty");
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		
	} 
	

}